<template>
  <div class="category">
    <!-- 面包屑导航 -->
    <Breadcrumb></Breadcrumb>
    <div class="wrapper">
      <div class="info">商品类目管理</div>
      <div class="tree">
        <!-- 一级按钮 -->
        <el-button type="warning" style="margin-bottom:20px;" @click="addCategory">新增一级导航类目</el-button>
        <!-- tree内容结构 -->
        <el-tree :data="data" show-checkbox node-key="id" default-expand-all :expand-on-click-node="false"
          :render-content="renderContent" :props="defaultProps">
        </el-tree>
      </div>
      <!-- 弹框 -->
      <Dialog :info="info" :type="type" ref="dialog" @updateview="itemCategory"></Dialog>
    </div>
  </div>
</template>

<script>
import Dialog from './Dialog.vue';
export default {
  components: { Dialog },
  data() {
    return {
      info: '标题',
      type: 1, //1：新增  2：修改 3：新增一级分类
      data: [{
        id: 1,
        name: '一级 1',
        children: [{
          id: 4,
          name: '二级 1-1'
        }]
      }],
      defaultProps: {
        label: 'name'
      }
    }
  },
  created() {
    this.itemCategory()
  },
  methods: {
    //新增一级类目
    addCategory(data){
      this.$refs.dialog.dialogVisible = true
      this.info = {
        title: `新增一级分类`,
        cid: data.cid
      }
      this.type = 3
      this.$refs.dialog.input = ''
    },
    //新增
    append(data) {
      this.$refs.dialog.dialogVisible = true
      this.info = {
        title: `新增【${data.name}】分类`,
        cid: data.cid
      }
      this.type = 1
      this.$refs.dialog.input = ''
    },
    //删除
    remove(node, data) {
      this.$confirm('是否删除当前类目', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除类目接口
        this.$api.deleteContentCategoryById({ id: data.id })
          .then(res => {
            if (res.data.status == 200) {
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
            this.itemCategory()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //修改
    update(node, data) {
      this.$refs.dialog.dialogVisible = true
      this.info = {
        title: `修改【${data.name}】名称`,
        id: data.id
      }
      this.type = 2
      this.$refs.dialog.input = data.name

    },
    // 树节点内容区渲染
    renderContent(h, { node, data, store }) {
      return (
        <span class="custom-tree-node">
          <span class="name">{node.label}</span>
          {/* 此处需用js语法  无法用if判断*/}
          {node.level == 1 ? <span>
            <el-button class='mini' on-click={() => this.append(data)} icon="el-icon-plus">新增</el-button>
            <el-button class='mini' on-click={() => this.update(node, data)} icon="el-icon-edit">修改</el-button>
            <el-button class='mini' type="danger" on-click={() => this.remove(node, data)} icon="el-icon-delete">删除</el-button>
          </span> : <span>
            <el-button class='mini' on-click={() => this.update(node, data)} icon="el-icon-edit">修改</el-button>
            <el-button class='mini' type="danger" on-click={() => this.remove(node, data)} icon="el-icon-delete">删除</el-button>
          </span>
          }
        </span>);
    },
    //请求tree数据
    async itemCategory() {
      let res = await this.$api.itemCategory()
      ////console.log(res.data.result);
      let data = res.data.result
      let arr1 = [], arr2 = []
      // 将一级节点，二级节点分开装入两个数组
      data.forEach(ele => {
        if (ele.type == 1) {
          ele.children = []
          arr1.push(ele)
        } else {
          arr2.push(ele)
        }
      });
      // 将二级节点装入一级节点
      arr1.forEach(item => {
        arr2.forEach(ele => {
          //一级节点中的type 与二级节点中的cid匹配
          if (ele.type === item.cid) {
            item.children.push(ele)
          }
        });
      });
      this.data = arr1
    }
  }
};
</script>

<style lang="less" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}

.wrapper {
  padding: 10px;
  margin-top: 10px;
  background: #fff;

  .info {
    background: #f6f6f6;
    height: 50px;
    line-height: 50px;
    font-weight: bold;
    padding-left: 10px;
  }

  .tree {
    margin: 10px;


    /deep/ .mini {
      padding: 4px 10px;
      font-size: 14px;
    }

    /deep/ .el-tree-node__content {
      margin-bottom: 10px;
    }
  }

  /deep/ .name {
    width: 300px;
    display: inline-block;
  }


}
</style>